//#include "common.h"

//#define LIST_NODE_DATATYPE filenode
#include "list.h"

// 初始化一个带头节点的双向链表
linklist init_list(void)
{
	linklist head = calloc(1, sizeof(listnode));
	if(head != NULL)
	{
		head->prev = head;	
		head->next = head;	
	}

	return head;
}

void list_add(linklist new, linklist head)
{
	new->prev = head;
	new->next = head->next;

	head->next->prev = new;
	head->next = new;
}

void list_add_tail(linklist new, linklist head)
{
	new->prev = head->prev;
	new->next = head;

	head->prev->next = new;
	head->prev = new;
}

void list_del(linklist del)
{
	del->prev->next = del->next;
	del->next->prev = del->prev;

	del->prev = NULL;
	del->next = NULL;
}

// 将节点list移动到head节点的后面
void list_move(linklist list, linklist head)
{
	list_del(list);
	list_add(list, head);
}

// 将节点list移动到head节点的前面
void list_move_tail(linklist list, linklist head)
{
	list_del(list);
	list_add_tail(list, head);
}

bool list_show(linklist head)          //遍历链表，一般在外部设计
{
	linklist tmp = head;
	if(head == NULL)
		return false;
	if(head->next == head)
		return false;
	if(head->prev == head)
		return false;
	for(;tmp->next!=head; tmp = tmp->next)
	{
		printf("%d\n",tmp->next->data);
	}
	return true;

}

linklist newnode(datatype data)         //新建节点，一般在外部设计
{
	linklist new = calloc(1,sizeof(listnode));
	new->data = data;
	new->next = new->prev = NULL;
	return new;
	
}
